package com.ruoyi.initialdata.tool;

import com.ruoyi.initialdata.domain.InitialDataTable1;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SQLParser {

    public List<InitialDataTable1> parseInsertStatements(String sql) {
        List<InitialDataTable1> dataList = new ArrayList<>();

        // 正则表达式匹配 INSERT 语句
        Pattern pattern = Pattern.compile("INSERT INTO initial_data_table_1\\s*\\((.*?)\\)\\s*VALUES\\s*\\((.*?)\\);");
        Matcher matcher = pattern.matcher(sql);

        while (matcher.find()) {
            String columns = matcher.group(1);
            String values = matcher.group(2);

            // 解析字段和值
            String[] columnNames = columns.split(",");
            String[] columnValues = values.split(",");

            // 创建 InitialDataTable1 对象并设置字段值
            InitialDataTable1 data = new InitialDataTable1();
            for (int i = 0; i < columnNames.length; i++) {
                String columnName = columnNames[i].trim();
                String columnValue = columnValues[i].trim().replace("'", "");
                //System.out.println(columnName);
                //System.out.println(columnValue);

                switch (columnName) {
                    case "data_id":
                        data.setDataId(Long.valueOf(columnValue));
                        break;
                    case "data_index_name":
                        data.setDataIndexName(columnValue);
                        break;
                    case "data_type":
                        data.setDataType(columnValue);
                        break;
                    case "data_number":
                        if (!columnValue.equals("NULL")) {
                            data.setDataNumber(columnValue);
                        }
                        break;
                    case "data_string":
                        if (!columnValue.equals("NULL")) {
                            data.setDataString(columnValue);
                        }
                        break;
                    case "data_time":
                        data.setDataTime(columnValue);
                        break;
                    case "data_content":
                        data.setDataContent(columnValue);
                        break;
                }
                //System.out.println(data);
            }

            dataList.add(data);
        }

        return dataList;
    }
}

